package com.anpy.bill.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.anpy.bill.mapper.UserMapper;
import com.anpy.bill.model.enums.LoginEnum;
import com.anpy.bill.model.vo.LoginRequestVO;
import com.anpy.bill.service.UserService;
import com.anpy.common.service.BaseServiceImpl;
import com.anpy.common.service.utils.RedisUtils;
import com.anpy.common.service.utils.TokenUtils;
import com.anpy.personal.JwtHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.personal.dao.entity.UserEntity;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("userServiceImpl")
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserEntity> implements UserService {

    @Override
    public boolean saveUserInfo(UserEntity userEntity) {
        return save(userEntity);
    }

    @Override
    public UserEntity loadUserInfo(Integer userId) {
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getId, userId);
        return this.getOne(wrapper);
    }

    @Override
    public boolean uploadUserInfo(UserEntity userEntity) {
        LambdaUpdateWrapper<UserEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserEntity::getHeaderPhoto, userEntity.getHeaderPhoto());
//        wrapper.set(UserEntity::getHeaderPhoto,userEntity.getHeaderPhoto());
        wrapper.eq(UserEntity::getId, userEntity.getId());
        return update(wrapper);
    }

    @Override
    public UserEntity loadCacheData(String phone) {
        String user = (String) RedisUtils.get(phone + "user");
        if (StrUtil.isEmpty(user)) {
            synchronized (this) {
                LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(UserEntity::getPhone, phone);
                UserEntity one = this.getOne(wrapper);

                RedisUtils.set(phone + "user", JSON.toJSONString(one), 1000 * 60 * 60 * 24 * 7);
                return one;
            }
        }

        return JSON.parseObject(user, new TypeReference<UserEntity>() {
        });
    }

    @Override
    public UserEntity login(LoginRequestVO userEntity) {
        switch (userEntity.getLoginType()) {
            //手机号码一键登录
            case PHONE_ONE_CLICK_LOGIN:
                if (ObjectUtils.isNotEmpty(userEntity.getPhone())) {
                    LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(UserEntity::getPhone, userEntity.getPhone());
                    UserEntity user = this.getOne(wrapper);
                    if (user == null) {
                        user = new UserEntity();
                        user.setPhone(userEntity.getPhone());
                        baseMapper.insert(user);
                    }

                    String token = JwtHelper.createToken(user.getId(), user.getPhone());
                    user.setToken(token);
                    return user;
                }
                break;
            //手机号验证码登录
            case PHONE_VERIFICATION:

                break;
            default:
                return null;
        }
        return null;
    }
}
